# -*- encoding=utf8 -*-

import os
import sys
import util

# 获取最新版本号
def findLastestVersion(path):
    if len(path) == 0:
        return ""
        
    versions = []
    for item in os.listdir(path):
        if os.path.isdir(path + "/" + item):
            versions.append(item)
    
    return util.ct_findBiggestVersion(versions)


# 遍历回调
def _callback(podName, path, tag):
    if tag == "search":
        if podName in podList:
            resultList.append({
                "name" : podName,
                "path" : path[len(targetFolder):],
                "lastestVersion" : findLastestVersion(specsFolder + podName)
            })


# 程序入口
if __name__ == "__main__":
    specsFolder = sys.path[0] + "/../Specs/"
    testFolder = sys.path[0] + "/../test/"

    podList = util.ct_getDirList(specsFolder)

    targetFolder = "../../00/cocoapods/Specs/"

    resultList = []
    util.ct_enumeratePodSpecs(path=targetFolder, callback=_callback, tag="search")

    # 根据仓库名进行排序
    resultList.sort(key=lambda e: e["name"].lower())

    util.ct_writeToFile("../search_index.json", resultList)